package com.qdevelop.control.action;

import java.util.List;
import java.util.Map;

import com.qdevelop.control.bean.QueryBean;
import com.qdevelop.control.bean.QueryConfigBean;
import com.qdevelop.control.bean.ResultBean;
import com.qdevelop.control.bean.RoleConfigBean;
import com.qdevelop.lang.IQdevelopConstant;
import com.qdevelop.model.service.database.DBFactory;
import com.qdevelop.model.utils.SQLConfigUtils;
import com.qdevelop.model.utils.UtilsFactory;

@SuppressWarnings("serial")
public class DataSecurityMaintainAction extends QDevelopBaseAction{
	
	private ResultBean rb;
	private ResultBean roles;
	private List<RoleConfigBean> roleList;
	private String index;
	private ResultBean json;
	private int cn;
	
	@SuppressWarnings("unused")
	private String _EHCACHE_CONFIG_NAME = "QDevelopCache_DataSecurityMaintainAction";


	public String showRoleList() throws Exception {

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String roleRelationList()throws Exception {
		Map t = this.getParamMap();
		t.put("index", "queryDataRoleList");
		QueryBean qb = UtilsFactory.qdevelopUtils().getQueryBean(new QueryConfigBean(t));
		rb = DBFactory.getInstance().getQueryResult(qb);

		QueryBean qb1 = UtilsFactory.qdevelopUtils().getQueryBean(new QueryConfigBean("quaryRoles"));
		roles = DBFactory.getInstance().getQueryResult(qb1);
		return SUCCESS;
	}

	ResultBean helpResultBean ;
	@SuppressWarnings("unchecked")
	public String dataSecurityMaintain()throws Exception {
		helpResultBean = DBFactory.getInstance().getQueryResult(UtilsFactory.qdevelopUtils().getQueryBean("quaryRoles"));
		roleList = SQLConfigUtils.getInstance().getRoleTableList();
		if(index!=null&&!index.equals("")){
			roles = DBFactory.getInstance().getQueryResult(UtilsFactory.qdevelopUtils().getQueryBean("quaryRoles"));
			Map t = this.getParamMap();
			QueryBean qb1 = UtilsFactory.qdevelopUtils().getQueryBean(t);
			t.put("index", "dateSecurityMaintainList");
			String tmp = null;
			for(int i=0;i<roleList.size();i++){
				if(roleList.get(i).getIndex().equals(index)){
					tmp = new StringBuffer().append(roleList.get(i).getRoleTable())
					.append(";").append(roleList.get(i).getRoleKey()).toString();
				}
			}
			t.put("param", tmp);
			QueryBean qb = UtilsFactory.qdevelopUtils().getQueryBean(t);
			qb.setSql(qb.getSQLByOrder().replace("#TABLE", qb1.getSQLByOrder()));
			json = DBFactory.getInstance().getQueryResult(qb);
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String getConfigByOwnRole()throws Exception{
		roleList = SQLConfigUtils.getInstance().getRoleTableList();
		return SUCCESS;
	}

	
	@SuppressWarnings("unchecked")
	public String updateDataSecurity() throws Exception {
		Map t = this.getParamMap();
		QueryBean qb1 = UtilsFactory.qdevelopUtils().getQueryBean((String)t.get("sIndex"));
		t.put("sTable", qb1.getDataSecurityTable());
		QueryBean[] qb = UtilsFactory.qdevelopUtils().getMultiQueryBean((t));
		cn = DBFactory.getInstance().updateBySQL(qb);
		String clearKey= SQLConfigUtils.getInstance().getIndexByRoleTable(qb1.getDataSecurityTable());
		//System.out.println("********"+clearKey);
		UtilsFactory.clearCache(clearKey, IQdevelopConstant._Result_Cache_Tactics_KEY);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public List getRoleList() {
		return roleList;
	}
	@SuppressWarnings("unchecked")
	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public ResultBean getRb() {
		return rb;
	}

	public void setRb(ResultBean rb) {
		this.rb = rb;
	}

	public ResultBean getRoles() {
		return roles;
	}

	public void setRoles(ResultBean roles) {
		this.roles = roles;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}
	public static void main(String[] args) {
		DataSecurityMaintainAction dsm = new DataSecurityMaintainAction();
		dsm.setIndex("queryDataByRole");
		try {
			dsm.dataSecurityMaintain();
			//			System.out.println(dsm.getRb());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ResultBean getHelpResultBean() {
		return helpResultBean;
	}

	public void setHelpResultBean(ResultBean helpResultBean) {
		this.helpResultBean = helpResultBean;
	}

	public ResultBean getJson() {
		return json;
	}

	public void setJson(ResultBean json) {
		this.json = json;
	}

	public int getCn() {
		return cn;
	}

	public void setCn(int cn) {
		this.cn = cn;
	}

}
